libiscsi.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  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 <asm/unaligned.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  47. #define SNA32_CHECK 2147483648UL
  48. static int iscsi_sna_lt(u32 n1, u32 n2)
  49. {
  50. return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  51. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  52. }
  53. /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
  54. static int iscsi_sna_lte(u32 n1, u32 n2)
  55. {
  56. return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
  57. (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
  58. }
  59. void
  60. iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  61. {
  62. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  63. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  64. /*
  65. * standard specifies this check for when to update expected and
  66. * max sequence numbers
  67. */
  68. if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
  69. return;
  70. if (exp_cmdsn != session->exp_cmdsn &&
  71. !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
  72. session->exp_cmdsn = exp_cmdsn;
  73. if (max_cmdsn != session->max_cmdsn &&
  74. !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
  75. session->max_cmdsn = max_cmdsn;
  76. /*
  77. * if the window closed with IO queued, then kick the
  78. * xmit thread
  79. */
  80. if (!list_empty(&session->leadconn->xmitqueue) ||
  81. !list_empty(&session->leadconn->mgmtqueue))
  82. scsi_queue_work(session->host,
  83. &session->leadconn->xmitwork);
  84. }
  85. }
  86. EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
  87. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  88. struct iscsi_data *hdr)
  89. {
  90. struct iscsi_conn *conn = ctask->conn;
  91. memset(hdr, 0, sizeof(struct iscsi_data));
  92. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  93. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  94. ctask->unsol_datasn++;
  95. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  96. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  97. hdr->itt = ctask->hdr->itt;
  98. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  99. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  100. if (ctask->unsol_count > conn->max_xmit_dlength) {
  101. hton24(hdr->dlength, conn->max_xmit_dlength);
  102. ctask->data_count = conn->max_xmit_dlength;
  103. ctask->unsol_offset += ctask->data_count;
  104. hdr->flags = 0;
  105. } else {
  106. hton24(hdr->dlength, ctask->unsol_count);
  107. ctask->data_count = ctask->unsol_count;
  108. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  112. static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
  113. {
  114. unsigned exp_len = ctask->hdr_len + len;
  115. if (exp_len > ctask->hdr_max) {
  116. WARN_ON(1);
  117. return -EINVAL;
  118. }
  119. WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
  120. ctask->hdr_len = exp_len;
  121. return 0;
  122. }
  123. /**
  124. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  125. * @ctask: iscsi cmd task
  126. *
  127. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  128. * fields like dlength or final based on how much data it sends
  129. */
  130. static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  131. {
  132. struct iscsi_conn *conn = ctask->conn;
  133. struct iscsi_session *session = conn->session;
  134. struct iscsi_cmd *hdr = ctask->hdr;
  135. struct scsi_cmnd *sc = ctask->sc;
  136. unsigned hdrlength;
  137. int rc;
  138. ctask->hdr_len = 0;
  139. rc = iscsi_add_hdr(ctask, sizeof(*hdr));
  140. if (rc)
  141. return rc;
  142. hdr->opcode = ISCSI_OP_SCSI_CMD;
  143. hdr->flags = ISCSI_ATTR_SIMPLE;
  144. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  145. hdr->itt = build_itt(ctask->itt, conn->id, session->age);
  146. hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
  147. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  148. session->cmdsn++;
  149. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  150. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  151. if (sc->cmd_len < MAX_COMMAND_SIZE)
  152. memset(&hdr->cdb[sc->cmd_len], 0,
  153. MAX_COMMAND_SIZE - sc->cmd_len);
  154. ctask->imm_count = 0;
  155. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  156. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  157. /*
  158. * Write counters:
  159. *
  160. * imm_count bytes to be sent right after
  161. * SCSI PDU Header
  162. *
  163. * unsol_count bytes(as Data-Out) to be sent
  164. * without R2T ack right after
  165. * immediate data
  166. *
  167. * r2t_data_count bytes to be sent via R2T ack's
  168. *
  169. * pad_count bytes to be sent as zero-padding
  170. */
  171. ctask->unsol_count = 0;
  172. ctask->unsol_offset = 0;
  173. ctask->unsol_datasn = 0;
  174. if (session->imm_data_en) {
  175. if (scsi_bufflen(sc) >= session->first_burst)
  176. ctask->imm_count = min(session->first_burst,
  177. conn->max_xmit_dlength);
  178. else
  179. ctask->imm_count = min(scsi_bufflen(sc),
  180. conn->max_xmit_dlength);
  181. hton24(hdr->dlength, ctask->imm_count);
  182. } else
  183. zero_data(hdr->dlength);
  184. if (!session->initial_r2t_en) {
  185. ctask->unsol_count = min((session->first_burst),
  186. (scsi_bufflen(sc))) - ctask->imm_count;
  187. ctask->unsol_offset = ctask->imm_count;
  188. }
  189. if (!ctask->unsol_count)
  190. /* No unsolicit Data-Out's */
  191. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  192. } else {
  193. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  194. zero_data(hdr->dlength);
  195. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  196. hdr->flags |= ISCSI_FLAG_CMD_READ;
  197. }
  198. /* calculate size of additional header segments (AHSs) */
  199. hdrlength = ctask->hdr_len - sizeof(*hdr);
  200. WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
  201. hdrlength /= ISCSI_PAD_LEN;
  202. WARN_ON(hdrlength >= 256);
  203. hdr->hlength = hdrlength & 0xFF;
  204. if (conn->session->tt->init_cmd_task(conn->ctask))
  205. return EIO;
  206. conn->scsicmd_pdus_cnt++;
  207. debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
  208. "cmdsn %d win %d]\n",
  209. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  210. conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
  211. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  212. return 0;
  213. }
  214. /**
  215. * iscsi_complete_command - return command back to scsi-ml
  216. * @ctask: iscsi cmd task
  217. *
  218. * Must be called with session lock.
  219. * This function returns the scsi command to scsi-ml and returns
  220. * the cmd task to the pool of available cmd tasks.
  221. */
  222. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  223. {
  224. struct iscsi_conn *conn = ctask->conn;
  225. struct iscsi_session *session = conn->session;
  226. struct scsi_cmnd *sc = ctask->sc;
  227. ctask->state = ISCSI_TASK_COMPLETED;
  228. ctask->sc = NULL;
  229. /* SCSI eh reuses commands to verify us */
  230. sc->SCp.ptr = NULL;
  231. if (conn->ctask == ctask)
  232. conn->ctask = NULL;
  233. list_del_init(&ctask->running);
  234. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  235. sc->scsi_done(sc);
  236. }
  237. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  238. {
  239. atomic_inc(&ctask->refcount);
  240. }
  241. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  242. {
  243. if (atomic_dec_and_test(&ctask->refcount))
  244. iscsi_complete_command(ctask);
  245. }
  246. /*
  247. * session lock must be held
  248. */
  249. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  250. int err)
  251. {
  252. struct scsi_cmnd *sc;
  253. sc = ctask->sc;
  254. if (!sc)
  255. return;
  256. if (ctask->state == ISCSI_TASK_PENDING)
  257. /*
  258. * cmd never made it to the xmit thread, so we should not count
  259. * the cmd in the sequencing
  260. */
  261. conn->session->queued_cmdsn--;
  262. else
  263. conn->session->tt->cleanup_cmd_task(conn, ctask);
  264. sc->result = err;
  265. scsi_set_resid(sc, scsi_bufflen(sc));
  266. if (conn->ctask == ctask)
  267. conn->ctask = NULL;
  268. /* release ref from queuecommand */
  269. __iscsi_put_ctask(ctask);
  270. }
  271. /**
  272. * iscsi_free_mgmt_task - return mgmt task back to pool
  273. * @conn: iscsi connection
  274. * @mtask: mtask
  275. *
  276. * Must be called with session lock.
  277. */
  278. void iscsi_free_mgmt_task(struct iscsi_conn *conn,
  279. struct iscsi_mgmt_task *mtask)
  280. {
  281. list_del_init(&mtask->running);
  282. if (conn->login_mtask == mtask)
  283. return;
  284. if (conn->ping_mtask == mtask)
  285. conn->ping_mtask = NULL;
  286. __kfifo_put(conn->session->mgmtpool.queue,
  287. (void*)&mtask, sizeof(void*));
  288. }
  289. EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
  290. static struct iscsi_mgmt_task *
  291. __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  292. char *data, uint32_t data_size)
  293. {
  294. struct iscsi_session *session = conn->session;
  295. struct iscsi_mgmt_task *mtask;
  296. if (session->state == ISCSI_STATE_TERMINATE)
  297. return NULL;
  298. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  299. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  300. /*
  301. * Login and Text are sent serially, in
  302. * request-followed-by-response sequence.
  303. * Same mtask can be used. Same ITT must be used.
  304. * Note that login_mtask is preallocated at conn_create().
  305. */
  306. mtask = conn->login_mtask;
  307. else {
  308. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  309. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  310. if (!__kfifo_get(session->mgmtpool.queue,
  311. (void*)&mtask, sizeof(void*)))
  312. return NULL;
  313. }
  314. if (data_size) {
  315. memcpy(mtask->data, data, data_size);
  316. mtask->data_count = data_size;
  317. } else
  318. mtask->data_count = 0;
  319. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  320. INIT_LIST_HEAD(&mtask->running);
  321. list_add_tail(&mtask->running, &conn->mgmtqueue);
  322. return mtask;
  323. }
  324. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  325. char *data, uint32_t data_size)
  326. {
  327. struct iscsi_conn *conn = cls_conn->dd_data;
  328. struct iscsi_session *session = conn->session;
  329. int err = 0;
  330. spin_lock_bh(&session->lock);
  331. if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
  332. err = -EPERM;
  333. spin_unlock_bh(&session->lock);
  334. scsi_queue_work(session->host, &conn->xmitwork);
  335. return err;
  336. }
  337. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  338. /**
  339. * iscsi_cmd_rsp - SCSI Command Response processing
  340. * @conn: iscsi connection
  341. * @hdr: iscsi header
  342. * @ctask: scsi command task
  343. * @data: cmd data buffer
  344. * @datalen: len of buffer
  345. *
  346. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  347. * then completes the command and task.
  348. **/
  349. static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  350. struct iscsi_cmd_task *ctask, char *data,
  351. int datalen)
  352. {
  353. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  354. struct iscsi_session *session = conn->session;
  355. struct scsi_cmnd *sc = ctask->sc;
  356. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  357. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  358. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  359. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  360. sc->result = DID_ERROR << 16;
  361. goto out;
  362. }
  363. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  364. uint16_t senselen;
  365. if (datalen < 2) {
  366. invalid_datalen:
  367. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  368. "invalid data buffer size of %d\n", datalen);
  369. sc->result = DID_BAD_TARGET << 16;
  370. goto out;
  371. }
  372. senselen = be16_to_cpu(get_unaligned((__be16 *) data));
  373. if (datalen < senselen)
  374. goto invalid_datalen;
  375. memcpy(sc->sense_buffer, data + 2,
  376. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  377. debug_scsi("copied %d bytes of sense\n",
  378. min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
  379. }
  380. if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
  381. ISCSI_FLAG_CMD_OVERFLOW)) {
  382. int res_count = be32_to_cpu(rhdr->residual_count);
  383. if (res_count > 0 &&
  384. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  385. res_count <= scsi_bufflen(sc)))
  386. scsi_set_resid(sc, res_count);
  387. else
  388. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  389. } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
  390. ISCSI_FLAG_CMD_BIDI_OVERFLOW))
  391. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  392. out:
  393. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  394. (long)sc, sc->result, ctask->itt);
  395. conn->scsirsp_pdus_cnt++;
  396. __iscsi_put_ctask(ctask);
  397. }
  398. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  399. {
  400. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  401. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  402. conn->tmfrsp_pdus_cnt++;
  403. if (conn->tmf_state != TMF_QUEUED)
  404. return;
  405. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  406. conn->tmf_state = TMF_SUCCESS;
  407. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  408. conn->tmf_state = TMF_NOT_FOUND;
  409. else
  410. conn->tmf_state = TMF_FAILED;
  411. wake_up(&conn->ehwait);
  412. }
  413. static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
  414. {
  415. struct iscsi_nopout hdr;
  416. struct iscsi_mgmt_task *mtask;
  417. if (!rhdr && conn->ping_mtask)
  418. return;
  419. memset(&hdr, 0, sizeof(struct iscsi_nopout));
  420. hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
  421. hdr.flags = ISCSI_FLAG_CMD_FINAL;
  422. if (rhdr) {
  423. memcpy(hdr.lun, rhdr->lun, 8);
  424. hdr.ttt = rhdr->ttt;
  425. hdr.itt = RESERVED_ITT;
  426. } else
  427. hdr.ttt = RESERVED_ITT;
  428. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
  429. if (!mtask) {
  430. printk(KERN_ERR "Could not send nopout\n");
  431. return;
  432. }
  433. /* only track our nops */
  434. if (!rhdr) {
  435. conn->ping_mtask = mtask;
  436. conn->last_ping = jiffies;
  437. }
  438. scsi_queue_work(conn->session->host, &conn->xmitwork);
  439. }
  440. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  441. char *data, int datalen)
  442. {
  443. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  444. struct iscsi_hdr rejected_pdu;
  445. uint32_t itt;
  446. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  447. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  448. if (ntoh24(reject->dlength) > datalen)
  449. return ISCSI_ERR_PROTO;
  450. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  451. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  452. itt = get_itt(rejected_pdu.itt);
  453. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  454. "due to DataDigest error.\n", itt,
  455. rejected_pdu.opcode);
  456. }
  457. }
  458. return 0;
  459. }
  460. /**
  461. * __iscsi_complete_pdu - complete pdu
  462. * @conn: iscsi conn
  463. * @hdr: iscsi header
  464. * @data: data buffer
  465. * @datalen: len of data buffer
  466. *
  467. * Completes pdu processing by freeing any resources allocated at
  468. * queuecommand or send generic. session lock must be held and verify
  469. * itt must have been called.
  470. */
  471. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  472. char *data, int datalen)
  473. {
  474. struct iscsi_session *session = conn->session;
  475. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  476. struct iscsi_cmd_task *ctask;
  477. struct iscsi_mgmt_task *mtask;
  478. uint32_t itt;
  479. conn->last_recv = jiffies;
  480. if (hdr->itt != RESERVED_ITT)
  481. itt = get_itt(hdr->itt);
  482. else
  483. itt = ~0U;
  484. if (itt < session->cmds_max) {
  485. ctask = session->cmds[itt];
  486. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  487. opcode, conn->id, ctask->itt, datalen);
  488. switch(opcode) {
  489. case ISCSI_OP_SCSI_CMD_RSP:
  490. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  491. iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  492. datalen);
  493. break;
  494. case ISCSI_OP_SCSI_DATA_IN:
  495. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  496. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  497. conn->scsirsp_pdus_cnt++;
  498. __iscsi_put_ctask(ctask);
  499. }
  500. break;
  501. case ISCSI_OP_R2T:
  502. /* LLD handles this for now */
  503. break;
  504. default:
  505. rc = ISCSI_ERR_BAD_OPCODE;
  506. break;
  507. }
  508. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  509. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  510. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  511. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  512. opcode, conn->id, mtask->itt, datalen);
  513. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  514. switch(opcode) {
  515. case ISCSI_OP_LOGOUT_RSP:
  516. if (datalen) {
  517. rc = ISCSI_ERR_PROTO;
  518. break;
  519. }
  520. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  521. /* fall through */
  522. case ISCSI_OP_LOGIN_RSP:
  523. case ISCSI_OP_TEXT_RSP:
  524. /*
  525. * login related PDU's exp_statsn is handled in
  526. * userspace
  527. */
  528. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  529. rc = ISCSI_ERR_CONN_FAILED;
  530. iscsi_free_mgmt_task(conn, mtask);
  531. break;
  532. case ISCSI_OP_SCSI_TMFUNC_RSP:
  533. if (datalen) {
  534. rc = ISCSI_ERR_PROTO;
  535. break;
  536. }
  537. iscsi_tmf_rsp(conn, hdr);
  538. iscsi_free_mgmt_task(conn, mtask);
  539. break;
  540. case ISCSI_OP_NOOP_IN:
  541. if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
  542. datalen) {
  543. rc = ISCSI_ERR_PROTO;
  544. break;
  545. }
  546. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  547. if (conn->ping_mtask != mtask) {
  548. /*
  549. * If this is not in response to one of our
  550. * nops then it must be from userspace.
  551. */
  552. if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
  553. datalen))
  554. rc = ISCSI_ERR_CONN_FAILED;
  555. }
  556. iscsi_free_mgmt_task(conn, mtask);
  557. break;
  558. default:
  559. rc = ISCSI_ERR_BAD_OPCODE;
  560. break;
  561. }
  562. } else if (itt == ~0U) {
  563. iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
  564. switch(opcode) {
  565. case ISCSI_OP_NOOP_IN:
  566. if (datalen) {
  567. rc = ISCSI_ERR_PROTO;
  568. break;
  569. }
  570. if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
  571. break;
  572. iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
  573. break;
  574. case ISCSI_OP_REJECT:
  575. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  576. break;
  577. case ISCSI_OP_ASYNC_EVENT:
  578. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  579. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  580. rc = ISCSI_ERR_CONN_FAILED;
  581. break;
  582. default:
  583. rc = ISCSI_ERR_BAD_OPCODE;
  584. break;
  585. }
  586. } else
  587. rc = ISCSI_ERR_BAD_ITT;
  588. return rc;
  589. }
  590. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  591. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  592. char *data, int datalen)
  593. {
  594. int rc;
  595. spin_lock(&conn->session->lock);
  596. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  597. spin_unlock(&conn->session->lock);
  598. return rc;
  599. }
  600. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  601. /* verify itt (itt encoding: age+cid+itt) */
  602. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  603. uint32_t *ret_itt)
  604. {
  605. struct iscsi_session *session = conn->session;
  606. struct iscsi_cmd_task *ctask;
  607. uint32_t itt;
  608. if (hdr->itt != RESERVED_ITT) {
  609. if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
  610. (session->age << ISCSI_AGE_SHIFT)) {
  611. printk(KERN_ERR "iscsi: received itt %x expected "
  612. "session age (%x)\n", (__force u32)hdr->itt,
  613. session->age & ISCSI_AGE_MASK);
  614. return ISCSI_ERR_BAD_ITT;
  615. }
  616. if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
  617. (conn->id << ISCSI_CID_SHIFT)) {
  618. printk(KERN_ERR "iscsi: received itt %x, expected "
  619. "CID (%x)\n", (__force u32)hdr->itt, conn->id);
  620. return ISCSI_ERR_BAD_ITT;
  621. }
  622. itt = get_itt(hdr->itt);
  623. } else
  624. itt = ~0U;
  625. if (itt < session->cmds_max) {
  626. ctask = session->cmds[itt];
  627. if (!ctask->sc) {
  628. printk(KERN_INFO "iscsi: dropping ctask with "
  629. "itt 0x%x\n", ctask->itt);
  630. /* force drop */
  631. return ISCSI_ERR_NO_SCSI_CMD;
  632. }
  633. if (ctask->sc->SCp.phase != session->age) {
  634. printk(KERN_ERR "iscsi: ctask's session age %d, "
  635. "expected %d\n", ctask->sc->SCp.phase,
  636. session->age);
  637. return ISCSI_ERR_SESSION_FAILED;
  638. }
  639. }
  640. *ret_itt = itt;
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  644. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  645. {
  646. struct iscsi_session *session = conn->session;
  647. unsigned long flags;
  648. spin_lock_irqsave(&session->lock, flags);
  649. if (session->state == ISCSI_STATE_FAILED) {
  650. spin_unlock_irqrestore(&session->lock, flags);
  651. return;
  652. }
  653. if (conn->stop_stage == 0)
  654. session->state = ISCSI_STATE_FAILED;
  655. spin_unlock_irqrestore(&session->lock, flags);
  656. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  657. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  658. iscsi_conn_error(conn->cls_conn, err);
  659. }
  660. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  661. static void iscsi_prep_mtask(struct iscsi_conn *conn,
  662. struct iscsi_mgmt_task *mtask)
  663. {
  664. struct iscsi_session *session = conn->session;
  665. struct iscsi_hdr *hdr = mtask->hdr;
  666. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  667. if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
  668. hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  669. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  670. /*
  671. * pre-format CmdSN for outgoing PDU.
  672. */
  673. nop->cmdsn = cpu_to_be32(session->cmdsn);
  674. if (hdr->itt != RESERVED_ITT) {
  675. hdr->itt = build_itt(mtask->itt, conn->id, session->age);
  676. /*
  677. * TODO: We always use immediate, so we never hit this.
  678. * If we start to send tmfs or nops as non-immediate then
  679. * we should start checking the cmdsn numbers for mgmt tasks.
  680. */
  681. if (conn->c_stage == ISCSI_CONN_STARTED &&
  682. !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
  683. session->queued_cmdsn++;
  684. session->cmdsn++;
  685. }
  686. }
  687. if (session->tt->init_mgmt_task)
  688. session->tt->init_mgmt_task(conn, mtask);
  689. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  690. hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
  691. mtask->data_count);
  692. }
  693. static int iscsi_xmit_mtask(struct iscsi_conn *conn)
  694. {
  695. struct iscsi_hdr *hdr = conn->mtask->hdr;
  696. int rc;
  697. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
  698. conn->session->state = ISCSI_STATE_LOGGING_OUT;
  699. spin_unlock_bh(&conn->session->lock);
  700. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  701. spin_lock_bh(&conn->session->lock);
  702. if (rc)
  703. return rc;
  704. /* done with this in-progress mtask */
  705. conn->mtask = NULL;
  706. return 0;
  707. }
  708. static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
  709. {
  710. struct iscsi_session *session = conn->session;
  711. /*
  712. * Check for iSCSI window and take care of CmdSN wrap-around
  713. */
  714. if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
  715. debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
  716. "CmdSN %u/%u\n", session->exp_cmdsn,
  717. session->max_cmdsn, session->cmdsn,
  718. session->queued_cmdsn);
  719. return -ENOSPC;
  720. }
  721. return 0;
  722. }
  723. static int iscsi_xmit_ctask(struct iscsi_conn *conn)
  724. {
  725. struct iscsi_cmd_task *ctask = conn->ctask;
  726. int rc;
  727. __iscsi_get_ctask(ctask);
  728. spin_unlock_bh(&conn->session->lock);
  729. rc = conn->session->tt->xmit_cmd_task(conn, ctask);
  730. spin_lock_bh(&conn->session->lock);
  731. __iscsi_put_ctask(ctask);
  732. if (!rc)
  733. /* done with this ctask */
  734. conn->ctask = NULL;
  735. return rc;
  736. }
  737. /**
  738. * iscsi_requeue_ctask - requeue ctask to run from session workqueue
  739. * @ctask: ctask to requeue
  740. *
  741. * LLDs that need to run a ctask from the session workqueue should call
  742. * this. The session lock must be held.
  743. */
  744. void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
  745. {
  746. struct iscsi_conn *conn = ctask->conn;
  747. list_move_tail(&ctask->running, &conn->requeue);
  748. scsi_queue_work(conn->session->host, &conn->xmitwork);
  749. }
  750. EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
  751. /**
  752. * iscsi_data_xmit - xmit any command into the scheduled connection
  753. * @conn: iscsi connection
  754. *
  755. * Notes:
  756. * The function can return -EAGAIN in which case the caller must
  757. * re-schedule it again later or recover. '0' return code means
  758. * successful xmit.
  759. **/
  760. static int iscsi_data_xmit(struct iscsi_conn *conn)
  761. {
  762. int rc = 0;
  763. spin_lock_bh(&conn->session->lock);
  764. if (unlikely(conn->suspend_tx)) {
  765. debug_scsi("conn %d Tx suspended!\n", conn->id);
  766. spin_unlock_bh(&conn->session->lock);
  767. return -ENODATA;
  768. }
  769. if (conn->ctask) {
  770. rc = iscsi_xmit_ctask(conn);
  771. if (rc)
  772. goto again;
  773. }
  774. if (conn->mtask) {
  775. rc = iscsi_xmit_mtask(conn);
  776. if (rc)
  777. goto again;
  778. }
  779. /*
  780. * process mgmt pdus like nops before commands since we should
  781. * only have one nop-out as a ping from us and targets should not
  782. * overflow us with nop-ins
  783. */
  784. check_mgmt:
  785. while (!list_empty(&conn->mgmtqueue)) {
  786. conn->mtask = list_entry(conn->mgmtqueue.next,
  787. struct iscsi_mgmt_task, running);
  788. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  789. iscsi_free_mgmt_task(conn, conn->mtask);
  790. conn->mtask = NULL;
  791. continue;
  792. }
  793. iscsi_prep_mtask(conn, conn->mtask);
  794. list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
  795. rc = iscsi_xmit_mtask(conn);
  796. if (rc)
  797. goto again;
  798. }
  799. /* process pending command queue */
  800. while (!list_empty(&conn->xmitqueue)) {
  801. if (conn->tmf_state == TMF_QUEUED)
  802. break;
  803. conn->ctask = list_entry(conn->xmitqueue.next,
  804. struct iscsi_cmd_task, running);
  805. if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
  806. fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
  807. continue;
  808. }
  809. if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
  810. fail_command(conn, conn->ctask, DID_ABORT << 16);
  811. continue;
  812. }
  813. conn->ctask->state = ISCSI_TASK_RUNNING;
  814. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  815. rc = iscsi_xmit_ctask(conn);
  816. if (rc)
  817. goto again;
  818. /*
  819. * we could continuously get new ctask requests so
  820. * we need to check the mgmt queue for nops that need to
  821. * be sent to aviod starvation
  822. */
  823. if (!list_empty(&conn->mgmtqueue))
  824. goto check_mgmt;
  825. }
  826. while (!list_empty(&conn->requeue)) {
  827. if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
  828. break;
  829. /*
  830. * we always do fastlogout - conn stop code will clean up.
  831. */
  832. if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
  833. break;
  834. conn->ctask = list_entry(conn->requeue.next,
  835. struct iscsi_cmd_task, running);
  836. conn->ctask->state = ISCSI_TASK_RUNNING;
  837. list_move_tail(conn->requeue.next, &conn->run_list);
  838. rc = iscsi_xmit_ctask(conn);
  839. if (rc)
  840. goto again;
  841. if (!list_empty(&conn->mgmtqueue))
  842. goto check_mgmt;
  843. }
  844. spin_unlock_bh(&conn->session->lock);
  845. return -ENODATA;
  846. again:
  847. if (unlikely(conn->suspend_tx))
  848. rc = -ENODATA;
  849. spin_unlock_bh(&conn->session->lock);
  850. return rc;
  851. }
  852. static void iscsi_xmitworker(struct work_struct *work)
  853. {
  854. struct iscsi_conn *conn =
  855. container_of(work, struct iscsi_conn, xmitwork);
  856. int rc;
  857. /*
  858. * serialize Xmit worker on a per-connection basis.
  859. */
  860. do {
  861. rc = iscsi_data_xmit(conn);
  862. } while (rc >= 0 || rc == -EAGAIN);
  863. }
  864. enum {
  865. FAILURE_BAD_HOST = 1,
  866. FAILURE_SESSION_FAILED,
  867. FAILURE_SESSION_FREED,
  868. FAILURE_WINDOW_CLOSED,
  869. FAILURE_OOM,
  870. FAILURE_SESSION_TERMINATE,
  871. FAILURE_SESSION_IN_RECOVERY,
  872. FAILURE_SESSION_RECOVERY_TIMEOUT,
  873. FAILURE_SESSION_LOGGING_OUT,
  874. };
  875. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  876. {
  877. struct Scsi_Host *host;
  878. int reason = 0;
  879. struct iscsi_session *session;
  880. struct iscsi_conn *conn;
  881. struct iscsi_cmd_task *ctask = NULL;
  882. sc->scsi_done = done;
  883. sc->result = 0;
  884. sc->SCp.ptr = NULL;
  885. host = sc->device->host;
  886. spin_unlock(host->host_lock);
  887. session = iscsi_hostdata(host->hostdata);
  888. spin_lock(&session->lock);
  889. /*
  890. * ISCSI_STATE_FAILED is a temp. state. The recovery
  891. * code will decide what is best to do with command queued
  892. * during this time
  893. */
  894. if (session->state != ISCSI_STATE_LOGGED_IN &&
  895. session->state != ISCSI_STATE_FAILED) {
  896. /*
  897. * to handle the race between when we set the recovery state
  898. * and block the session we requeue here (commands could
  899. * be entering our queuecommand while a block is starting
  900. * up because the block code is not locked)
  901. */
  902. switch (session->state) {
  903. case ISCSI_STATE_IN_RECOVERY:
  904. reason = FAILURE_SESSION_IN_RECOVERY;
  905. goto reject;
  906. case ISCSI_STATE_LOGGING_OUT:
  907. reason = FAILURE_SESSION_LOGGING_OUT;
  908. goto reject;
  909. case ISCSI_STATE_RECOVERY_FAILED:
  910. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  911. break;
  912. case ISCSI_STATE_TERMINATE:
  913. reason = FAILURE_SESSION_TERMINATE;
  914. break;
  915. default:
  916. reason = FAILURE_SESSION_FREED;
  917. }
  918. goto fault;
  919. }
  920. conn = session->leadconn;
  921. if (!conn) {
  922. reason = FAILURE_SESSION_FREED;
  923. goto fault;
  924. }
  925. if (iscsi_check_cmdsn_window_closed(conn)) {
  926. reason = FAILURE_WINDOW_CLOSED;
  927. goto reject;
  928. }
  929. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  930. sizeof(void*))) {
  931. reason = FAILURE_OOM;
  932. goto reject;
  933. }
  934. session->queued_cmdsn++;
  935. sc->SCp.phase = session->age;
  936. sc->SCp.ptr = (char *)ctask;
  937. atomic_set(&ctask->refcount, 1);
  938. ctask->state = ISCSI_TASK_PENDING;
  939. ctask->conn = conn;
  940. ctask->sc = sc;
  941. INIT_LIST_HEAD(&ctask->running);
  942. list_add_tail(&ctask->running, &conn->xmitqueue);
  943. spin_unlock(&session->lock);
  944. scsi_queue_work(host, &conn->xmitwork);
  945. spin_lock(host->host_lock);
  946. return 0;
  947. reject:
  948. spin_unlock(&session->lock);
  949. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  950. spin_lock(host->host_lock);
  951. return SCSI_MLQUEUE_HOST_BUSY;
  952. fault:
  953. spin_unlock(&session->lock);
  954. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  955. sc->cmnd[0], reason);
  956. sc->result = (DID_NO_CONNECT << 16);
  957. scsi_set_resid(sc, scsi_bufflen(sc));
  958. sc->scsi_done(sc);
  959. spin_lock(host->host_lock);
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  963. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  964. {
  965. if (depth > ISCSI_MAX_CMD_PER_LUN)
  966. depth = ISCSI_MAX_CMD_PER_LUN;
  967. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  968. return sdev->queue_depth;
  969. }
  970. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  971. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  972. {
  973. struct iscsi_session *session = class_to_transport_session(cls_session);
  974. spin_lock_bh(&session->lock);
  975. if (session->state != ISCSI_STATE_LOGGED_IN) {
  976. session->state = ISCSI_STATE_RECOVERY_FAILED;
  977. if (session->leadconn)
  978. wake_up(&session->leadconn->ehwait);
  979. }
  980. spin_unlock_bh(&session->lock);
  981. }
  982. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  983. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  984. {
  985. struct Scsi_Host *host = sc->device->host;
  986. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  987. struct iscsi_conn *conn = session->leadconn;
  988. mutex_lock(&session->eh_mutex);
  989. spin_lock_bh(&session->lock);
  990. if (session->state == ISCSI_STATE_TERMINATE) {
  991. failed:
  992. debug_scsi("failing host reset: session terminated "
  993. "[CID %d age %d]\n", conn->id, session->age);
  994. spin_unlock_bh(&session->lock);
  995. mutex_unlock(&session->eh_mutex);
  996. return FAILED;
  997. }
  998. spin_unlock_bh(&session->lock);
  999. mutex_unlock(&session->eh_mutex);
  1000. /*
  1001. * we drop the lock here but the leadconn cannot be destoyed while
  1002. * we are in the scsi eh
  1003. */
  1004. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1005. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  1006. wait_event_interruptible(conn->ehwait,
  1007. session->state == ISCSI_STATE_TERMINATE ||
  1008. session->state == ISCSI_STATE_LOGGED_IN ||
  1009. session->state == ISCSI_STATE_RECOVERY_FAILED);
  1010. if (signal_pending(current))
  1011. flush_signals(current);
  1012. mutex_lock(&session->eh_mutex);
  1013. spin_lock_bh(&session->lock);
  1014. if (session->state == ISCSI_STATE_LOGGED_IN)
  1015. printk(KERN_INFO "iscsi: host reset succeeded\n");
  1016. else
  1017. goto failed;
  1018. spin_unlock_bh(&session->lock);
  1019. mutex_unlock(&session->eh_mutex);
  1020. return SUCCESS;
  1021. }
  1022. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  1023. static void iscsi_tmf_timedout(unsigned long data)
  1024. {
  1025. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1026. struct iscsi_session *session = conn->session;
  1027. spin_lock(&session->lock);
  1028. if (conn->tmf_state == TMF_QUEUED) {
  1029. conn->tmf_state = TMF_TIMEDOUT;
  1030. debug_scsi("tmf timedout\n");
  1031. /* unblock eh_abort() */
  1032. wake_up(&conn->ehwait);
  1033. }
  1034. spin_unlock(&session->lock);
  1035. }
  1036. static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
  1037. struct iscsi_tm *hdr, int age,
  1038. int timeout)
  1039. {
  1040. struct iscsi_session *session = conn->session;
  1041. struct iscsi_mgmt_task *mtask;
  1042. mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
  1043. NULL, 0);
  1044. if (!mtask) {
  1045. spin_unlock_bh(&session->lock);
  1046. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1047. spin_lock_bh(&session->lock);
  1048. debug_scsi("tmf exec failure\n");
  1049. return -EPERM;
  1050. }
  1051. conn->tmfcmd_pdus_cnt++;
  1052. conn->tmf_timer.expires = timeout * HZ + jiffies;
  1053. conn->tmf_timer.function = iscsi_tmf_timedout;
  1054. conn->tmf_timer.data = (unsigned long)conn;
  1055. add_timer(&conn->tmf_timer);
  1056. debug_scsi("tmf set timeout\n");
  1057. spin_unlock_bh(&session->lock);
  1058. mutex_unlock(&session->eh_mutex);
  1059. scsi_queue_work(session->host, &conn->xmitwork);
  1060. /*
  1061. * block eh thread until:
  1062. *
  1063. * 1) tmf response
  1064. * 2) tmf timeout
  1065. * 3) session is terminated or restarted or userspace has
  1066. * given up on recovery
  1067. */
  1068. wait_event_interruptible(conn->ehwait, age != session->age ||
  1069. session->state != ISCSI_STATE_LOGGED_IN ||
  1070. conn->tmf_state != TMF_QUEUED);
  1071. if (signal_pending(current))
  1072. flush_signals(current);
  1073. del_timer_sync(&conn->tmf_timer);
  1074. mutex_lock(&session->eh_mutex);
  1075. spin_lock_bh(&session->lock);
  1076. /* if the session drops it will clean up the mtask */
  1077. if (age != session->age ||
  1078. session->state != ISCSI_STATE_LOGGED_IN)
  1079. return -ENOTCONN;
  1080. return 0;
  1081. }
  1082. /*
  1083. * Fail commands. session lock held and recv side suspended and xmit
  1084. * thread flushed
  1085. */
  1086. static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
  1087. {
  1088. struct iscsi_cmd_task *ctask, *tmp;
  1089. if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
  1090. conn->ctask = NULL;
  1091. /* flush pending */
  1092. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1093. if (lun == ctask->sc->device->lun || lun == -1) {
  1094. debug_scsi("failing pending sc %p itt 0x%x\n",
  1095. ctask->sc, ctask->itt);
  1096. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1097. }
  1098. }
  1099. list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
  1100. if (lun == ctask->sc->device->lun || lun == -1) {
  1101. debug_scsi("failing requeued sc %p itt 0x%x\n",
  1102. ctask->sc, ctask->itt);
  1103. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1104. }
  1105. }
  1106. /* fail all other running */
  1107. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1108. if (lun == ctask->sc->device->lun || lun == -1) {
  1109. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1110. ctask->sc, ctask->itt);
  1111. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1112. }
  1113. }
  1114. }
  1115. static void iscsi_suspend_tx(struct iscsi_conn *conn)
  1116. {
  1117. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1118. scsi_flush_work(conn->session->host);
  1119. }
  1120. static void iscsi_start_tx(struct iscsi_conn *conn)
  1121. {
  1122. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1123. scsi_queue_work(conn->session->host, &conn->xmitwork);
  1124. }
  1125. static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
  1126. {
  1127. struct iscsi_cls_session *cls_session;
  1128. struct iscsi_session *session;
  1129. struct iscsi_conn *conn;
  1130. enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
  1131. cls_session = starget_to_session(scsi_target(scmd->device));
  1132. session = class_to_transport_session(cls_session);
  1133. debug_scsi("scsi cmd %p timedout\n", scmd);
  1134. spin_lock(&session->lock);
  1135. if (session->state != ISCSI_STATE_LOGGED_IN) {
  1136. /*
  1137. * We are probably in the middle of iscsi recovery so let
  1138. * that complete and handle the error.
  1139. */
  1140. rc = EH_RESET_TIMER;
  1141. goto done;
  1142. }
  1143. conn = session->leadconn;
  1144. if (!conn) {
  1145. /* In the middle of shuting down */
  1146. rc = EH_RESET_TIMER;
  1147. goto done;
  1148. }
  1149. if (!conn->recv_timeout && !conn->ping_timeout)
  1150. goto done;
  1151. /*
  1152. * if the ping timedout then we are in the middle of cleaning up
  1153. * and can let the iscsi eh handle it
  1154. */
  1155. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
  1156. (conn->ping_timeout * HZ), jiffies))
  1157. rc = EH_RESET_TIMER;
  1158. /*
  1159. * if we are about to check the transport then give the command
  1160. * more time
  1161. */
  1162. if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
  1163. jiffies))
  1164. rc = EH_RESET_TIMER;
  1165. /* if in the middle of checking the transport then give us more time */
  1166. if (conn->ping_mtask)
  1167. rc = EH_RESET_TIMER;
  1168. done:
  1169. spin_unlock(&session->lock);
  1170. debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
  1171. return rc;
  1172. }
  1173. static void iscsi_check_transport_timeouts(unsigned long data)
  1174. {
  1175. struct iscsi_conn *conn = (struct iscsi_conn *)data;
  1176. struct iscsi_session *session = conn->session;
  1177. unsigned long timeout, next_timeout = 0, last_recv;
  1178. spin_lock(&session->lock);
  1179. if (session->state != ISCSI_STATE_LOGGED_IN)
  1180. goto done;
  1181. timeout = conn->recv_timeout;
  1182. if (!timeout)
  1183. goto done;
  1184. timeout *= HZ;
  1185. last_recv = conn->last_recv;
  1186. if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
  1187. jiffies)) {
  1188. printk(KERN_ERR "ping timeout of %d secs expired, "
  1189. "last rx %lu, last ping %lu, now %lu\n",
  1190. conn->ping_timeout, last_recv,
  1191. conn->last_ping, jiffies);
  1192. spin_unlock(&session->lock);
  1193. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1194. return;
  1195. }
  1196. if (time_before_eq(last_recv + timeout, jiffies)) {
  1197. if (time_before_eq(conn->last_ping, last_recv)) {
  1198. /* send a ping to try to provoke some traffic */
  1199. debug_scsi("Sending nopout as ping on conn %p\n", conn);
  1200. iscsi_send_nopout(conn, NULL);
  1201. }
  1202. next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
  1203. } else {
  1204. next_timeout = last_recv + timeout;
  1205. }
  1206. if (next_timeout) {
  1207. debug_scsi("Setting next tmo %lu\n", next_timeout);
  1208. mod_timer(&conn->transport_timer, next_timeout);
  1209. }
  1210. done:
  1211. spin_unlock(&session->lock);
  1212. }
  1213. static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
  1214. struct iscsi_tm *hdr)
  1215. {
  1216. memset(hdr, 0, sizeof(*hdr));
  1217. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1218. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
  1219. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1220. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1221. hdr->rtt = ctask->hdr->itt;
  1222. hdr->refcmdsn = ctask->hdr->cmdsn;
  1223. }
  1224. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1225. {
  1226. struct Scsi_Host *host = sc->device->host;
  1227. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1228. struct iscsi_conn *conn;
  1229. struct iscsi_cmd_task *ctask;
  1230. struct iscsi_tm *hdr;
  1231. int rc, age;
  1232. mutex_lock(&session->eh_mutex);
  1233. spin_lock_bh(&session->lock);
  1234. /*
  1235. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1236. * got the command.
  1237. */
  1238. if (!sc->SCp.ptr) {
  1239. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1240. spin_unlock_bh(&session->lock);
  1241. mutex_unlock(&session->eh_mutex);
  1242. return SUCCESS;
  1243. }
  1244. /*
  1245. * If we are not logged in or we have started a new session
  1246. * then let the host reset code handle this
  1247. */
  1248. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
  1249. sc->SCp.phase != session->age) {
  1250. spin_unlock_bh(&session->lock);
  1251. mutex_unlock(&session->eh_mutex);
  1252. return FAILED;
  1253. }
  1254. conn = session->leadconn;
  1255. conn->eh_abort_cnt++;
  1256. age = session->age;
  1257. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1258. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1259. /* ctask completed before time out */
  1260. if (!ctask->sc) {
  1261. debug_scsi("sc completed while abort in progress\n");
  1262. goto success;
  1263. }
  1264. if (ctask->state == ISCSI_TASK_PENDING) {
  1265. fail_command(conn, ctask, DID_ABORT << 16);
  1266. goto success;
  1267. }
  1268. /* only have one tmf outstanding at a time */
  1269. if (conn->tmf_state != TMF_INITIAL)
  1270. goto failed;
  1271. conn->tmf_state = TMF_QUEUED;
  1272. hdr = &conn->tmhdr;
  1273. iscsi_prep_abort_task_pdu(ctask, hdr);
  1274. if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
  1275. rc = FAILED;
  1276. goto failed;
  1277. }
  1278. switch (conn->tmf_state) {
  1279. case TMF_SUCCESS:
  1280. spin_unlock_bh(&session->lock);
  1281. iscsi_suspend_tx(conn);
  1282. /*
  1283. * clean up task if aborted. grab the recv lock as a writer
  1284. */
  1285. write_lock_bh(conn->recv_lock);
  1286. spin_lock(&session->lock);
  1287. fail_command(conn, ctask, DID_ABORT << 16);
  1288. conn->tmf_state = TMF_INITIAL;
  1289. spin_unlock(&session->lock);
  1290. write_unlock_bh(conn->recv_lock);
  1291. iscsi_start_tx(conn);
  1292. goto success_unlocked;
  1293. case TMF_TIMEDOUT:
  1294. spin_unlock_bh(&session->lock);
  1295. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1296. goto failed_unlocked;
  1297. case TMF_NOT_FOUND:
  1298. if (!sc->SCp.ptr) {
  1299. conn->tmf_state = TMF_INITIAL;
  1300. /* ctask completed before tmf abort response */
  1301. debug_scsi("sc completed while abort in progress\n");
  1302. goto success;
  1303. }
  1304. /* fall through */
  1305. default:
  1306. conn->tmf_state = TMF_INITIAL;
  1307. goto failed;
  1308. }
  1309. success:
  1310. spin_unlock_bh(&session->lock);
  1311. success_unlocked:
  1312. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1313. mutex_unlock(&session->eh_mutex);
  1314. return SUCCESS;
  1315. failed:
  1316. spin_unlock_bh(&session->lock);
  1317. failed_unlocked:
  1318. debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
  1319. ctask ? ctask->itt : 0);
  1320. mutex_unlock(&session->eh_mutex);
  1321. return FAILED;
  1322. }
  1323. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1324. static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
  1325. {
  1326. memset(hdr, 0, sizeof(*hdr));
  1327. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  1328. hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
  1329. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  1330. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  1331. hdr->rtt = RESERVED_ITT;
  1332. }
  1333. int iscsi_eh_device_reset(struct scsi_cmnd *sc)
  1334. {
  1335. struct Scsi_Host *host = sc->device->host;
  1336. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  1337. struct iscsi_conn *conn;
  1338. struct iscsi_tm *hdr;
  1339. int rc = FAILED;
  1340. debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
  1341. mutex_lock(&session->eh_mutex);
  1342. spin_lock_bh(&session->lock);
  1343. /*
  1344. * Just check if we are not logged in. We cannot check for
  1345. * the phase because the reset could come from a ioctl.
  1346. */
  1347. if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
  1348. goto unlock;
  1349. conn = session->leadconn;
  1350. /* only have one tmf outstanding at a time */
  1351. if (conn->tmf_state != TMF_INITIAL)
  1352. goto unlock;
  1353. conn->tmf_state = TMF_QUEUED;
  1354. hdr = &conn->tmhdr;
  1355. iscsi_prep_lun_reset_pdu(sc, hdr);
  1356. if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
  1357. session->lu_reset_timeout)) {
  1358. rc = FAILED;
  1359. goto unlock;
  1360. }
  1361. switch (conn->tmf_state) {
  1362. case TMF_SUCCESS:
  1363. break;
  1364. case TMF_TIMEDOUT:
  1365. spin_unlock_bh(&session->lock);
  1366. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1367. goto done;
  1368. default:
  1369. conn->tmf_state = TMF_INITIAL;
  1370. goto unlock;
  1371. }
  1372. rc = SUCCESS;
  1373. spin_unlock_bh(&session->lock);
  1374. iscsi_suspend_tx(conn);
  1375. /* need to grab the recv lock then session lock */
  1376. write_lock_bh(conn->recv_lock);
  1377. spin_lock(&session->lock);
  1378. fail_all_commands(conn, sc->device->lun);
  1379. conn->tmf_state = TMF_INITIAL;
  1380. spin_unlock(&session->lock);
  1381. write_unlock_bh(conn->recv_lock);
  1382. iscsi_start_tx(conn);
  1383. goto done;
  1384. unlock:
  1385. spin_unlock_bh(&session->lock);
  1386. done:
  1387. debug_scsi("iscsi_eh_device_reset %s\n",
  1388. rc == SUCCESS ? "SUCCESS" : "FAILED");
  1389. mutex_unlock(&session->eh_mutex);
  1390. return rc;
  1391. }
  1392. EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
  1393. /*
  1394. * Pre-allocate a pool of @max items of @item_size. By default, the pool
  1395. * should be accessed via kfifo_{get,put} on q->queue.
  1396. * Optionally, the caller can obtain the array of object pointers
  1397. * by passing in a non-NULL @items pointer
  1398. */
  1399. int
  1400. iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
  1401. {
  1402. int i, num_arrays = 1;
  1403. memset(q, 0, sizeof(*q));
  1404. q->max = max;
  1405. /* If the user passed an items pointer, he wants a copy of
  1406. * the array. */
  1407. if (items)
  1408. num_arrays++;
  1409. q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
  1410. if (q->pool == NULL)
  1411. goto enomem;
  1412. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1413. GFP_KERNEL, NULL);
  1414. if (q->queue == ERR_PTR(-ENOMEM))
  1415. goto enomem;
  1416. for (i = 0; i < max; i++) {
  1417. q->pool[i] = kzalloc(item_size, GFP_KERNEL);
  1418. if (q->pool[i] == NULL) {
  1419. q->max = i;
  1420. goto enomem;
  1421. }
  1422. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1423. }
  1424. if (items) {
  1425. *items = q->pool + max;
  1426. memcpy(*items, q->pool, max * sizeof(void *));
  1427. }
  1428. return 0;
  1429. enomem:
  1430. iscsi_pool_free(q);
  1431. return -ENOMEM;
  1432. }
  1433. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1434. void iscsi_pool_free(struct iscsi_pool *q)
  1435. {
  1436. int i;
  1437. for (i = 0; i < q->max; i++)
  1438. kfree(q->pool[i]);
  1439. if (q->pool)
  1440. kfree(q->pool);
  1441. }
  1442. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1443. /*
  1444. * iSCSI Session's hostdata organization:
  1445. *
  1446. * *------------------* <== hostdata_session(host->hostdata)
  1447. * | ptr to class sess|
  1448. * |------------------| <== iscsi_hostdata(host->hostdata)
  1449. * | iscsi_session |
  1450. * *------------------*
  1451. */
  1452. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1453. _sz % sizeof(unsigned long))
  1454. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1455. /**
  1456. * iscsi_session_setup - create iscsi cls session and host and session
  1457. * @scsit: scsi transport template
  1458. * @iscsit: iscsi transport template
  1459. * @cmds_max: scsi host can queue
  1460. * @qdepth: scsi host cmds per lun
  1461. * @cmd_task_size: LLD ctask private data size
  1462. * @mgmt_task_size: LLD mtask private data size
  1463. * @initial_cmdsn: initial CmdSN
  1464. * @hostno: host no allocated
  1465. *
  1466. * This can be used by software iscsi_transports that allocate
  1467. * a session per scsi host.
  1468. **/
  1469. struct iscsi_cls_session *
  1470. iscsi_session_setup(struct iscsi_transport *iscsit,
  1471. struct scsi_transport_template *scsit,
  1472. uint16_t cmds_max, uint16_t qdepth,
  1473. int cmd_task_size, int mgmt_task_size,
  1474. uint32_t initial_cmdsn, uint32_t *hostno)
  1475. {
  1476. struct Scsi_Host *shost;
  1477. struct iscsi_session *session;
  1478. struct iscsi_cls_session *cls_session;
  1479. int cmd_i;
  1480. if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
  1481. if (qdepth != 0)
  1482. printk(KERN_ERR "iscsi: invalid queue depth of %d. "
  1483. "Queue depth must be between 1 and %d.\n",
  1484. qdepth, ISCSI_MAX_CMD_PER_LUN);
  1485. qdepth = ISCSI_DEF_CMD_PER_LUN;
  1486. }
  1487. if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
  1488. cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
  1489. if (cmds_max != 0)
  1490. printk(KERN_ERR "iscsi: invalid can_queue of %d. "
  1491. "can_queue must be a power of 2 and between "
  1492. "2 and %d - setting to %d.\n", cmds_max,
  1493. ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
  1494. cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
  1495. }
  1496. shost = scsi_host_alloc(iscsit->host_template,
  1497. hostdata_privsize(sizeof(*session)));
  1498. if (!shost)
  1499. return NULL;
  1500. /* the iscsi layer takes one task for reserve */
  1501. shost->can_queue = cmds_max - 1;
  1502. shost->cmd_per_lun = qdepth;
  1503. shost->max_id = 1;
  1504. shost->max_channel = 0;
  1505. shost->max_lun = iscsit->max_lun;
  1506. shost->max_cmd_len = iscsit->max_cmd_len;
  1507. shost->transportt = scsit;
  1508. shost->transportt->create_work_queue = 1;
  1509. shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
  1510. *hostno = shost->host_no;
  1511. session = iscsi_hostdata(shost->hostdata);
  1512. memset(session, 0, sizeof(struct iscsi_session));
  1513. session->host = shost;
  1514. session->state = ISCSI_STATE_FREE;
  1515. session->fast_abort = 1;
  1516. session->lu_reset_timeout = 15;
  1517. session->abort_timeout = 10;
  1518. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1519. session->cmds_max = cmds_max;
  1520. session->queued_cmdsn = session->cmdsn = initial_cmdsn;
  1521. session->exp_cmdsn = initial_cmdsn + 1;
  1522. session->max_cmdsn = initial_cmdsn + 1;
  1523. session->max_r2t = 1;
  1524. session->tt = iscsit;
  1525. mutex_init(&session->eh_mutex);
  1526. /* initialize SCSI PDU commands pool */
  1527. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1528. (void***)&session->cmds,
  1529. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1530. goto cmdpool_alloc_fail;
  1531. /* pre-format cmds pool with ITT */
  1532. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1533. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1534. if (cmd_task_size)
  1535. ctask->dd_data = &ctask[1];
  1536. ctask->itt = cmd_i;
  1537. INIT_LIST_HEAD(&ctask->running);
  1538. }
  1539. spin_lock_init(&session->lock);
  1540. /* initialize immediate command pool */
  1541. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1542. (void***)&session->mgmt_cmds,
  1543. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1544. goto mgmtpool_alloc_fail;
  1545. /* pre-format immediate cmds pool with ITT */
  1546. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1547. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1548. if (mgmt_task_size)
  1549. mtask->dd_data = &mtask[1];
  1550. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1551. INIT_LIST_HEAD(&mtask->running);
  1552. }
  1553. if (scsi_add_host(shost, NULL))
  1554. goto add_host_fail;
  1555. if (!try_module_get(iscsit->owner))
  1556. goto cls_session_fail;
  1557. cls_session = iscsi_create_session(shost, iscsit, 0);
  1558. if (!cls_session)
  1559. goto module_put;
  1560. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1561. return cls_session;
  1562. module_put:
  1563. module_put(iscsit->owner);
  1564. cls_session_fail:
  1565. scsi_remove_host(shost);
  1566. add_host_fail:
  1567. iscsi_pool_free(&session->mgmtpool);
  1568. mgmtpool_alloc_fail:
  1569. iscsi_pool_free(&session->cmdpool);
  1570. cmdpool_alloc_fail:
  1571. scsi_host_put(shost);
  1572. return NULL;
  1573. }
  1574. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1575. /**
  1576. * iscsi_session_teardown - destroy session, host, and cls_session
  1577. * shost: scsi host
  1578. *
  1579. * This can be used by software iscsi_transports that allocate
  1580. * a session per scsi host.
  1581. **/
  1582. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1583. {
  1584. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1585. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1586. struct module *owner = cls_session->transport->owner;
  1587. iscsi_remove_session(cls_session);
  1588. scsi_remove_host(shost);
  1589. iscsi_pool_free(&session->mgmtpool);
  1590. iscsi_pool_free(&session->cmdpool);
  1591. kfree(session->password);
  1592. kfree(session->password_in);
  1593. kfree(session->username);
  1594. kfree(session->username_in);
  1595. kfree(session->targetname);
  1596. kfree(session->netdev);
  1597. kfree(session->hwaddress);
  1598. kfree(session->initiatorname);
  1599. iscsi_free_session(cls_session);
  1600. scsi_host_put(shost);
  1601. module_put(owner);
  1602. }
  1603. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1604. /**
  1605. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1606. * @cls_session: iscsi_cls_session
  1607. * @conn_idx: cid
  1608. **/
  1609. struct iscsi_cls_conn *
  1610. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1611. {
  1612. struct iscsi_session *session = class_to_transport_session(cls_session);
  1613. struct iscsi_conn *conn;
  1614. struct iscsi_cls_conn *cls_conn;
  1615. char *data;
  1616. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1617. if (!cls_conn)
  1618. return NULL;
  1619. conn = cls_conn->dd_data;
  1620. memset(conn, 0, sizeof(*conn));
  1621. conn->session = session;
  1622. conn->cls_conn = cls_conn;
  1623. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1624. conn->id = conn_idx;
  1625. conn->exp_statsn = 0;
  1626. conn->tmf_state = TMF_INITIAL;
  1627. init_timer(&conn->transport_timer);
  1628. conn->transport_timer.data = (unsigned long)conn;
  1629. conn->transport_timer.function = iscsi_check_transport_timeouts;
  1630. INIT_LIST_HEAD(&conn->run_list);
  1631. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1632. INIT_LIST_HEAD(&conn->mgmtqueue);
  1633. INIT_LIST_HEAD(&conn->xmitqueue);
  1634. INIT_LIST_HEAD(&conn->requeue);
  1635. INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
  1636. /* allocate login_mtask used for the login/text sequences */
  1637. spin_lock_bh(&session->lock);
  1638. if (!__kfifo_get(session->mgmtpool.queue,
  1639. (void*)&conn->login_mtask,
  1640. sizeof(void*))) {
  1641. spin_unlock_bh(&session->lock);
  1642. goto login_mtask_alloc_fail;
  1643. }
  1644. spin_unlock_bh(&session->lock);
  1645. data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
  1646. if (!data)
  1647. goto login_mtask_data_alloc_fail;
  1648. conn->login_mtask->data = conn->data = data;
  1649. init_timer(&conn->tmf_timer);
  1650. init_waitqueue_head(&conn->ehwait);
  1651. return cls_conn;
  1652. login_mtask_data_alloc_fail:
  1653. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1654. sizeof(void*));
  1655. login_mtask_alloc_fail:
  1656. iscsi_destroy_conn(cls_conn);
  1657. return NULL;
  1658. }
  1659. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1660. /**
  1661. * iscsi_conn_teardown - teardown iscsi connection
  1662. * cls_conn: iscsi class connection
  1663. *
  1664. * TODO: we may need to make this into a two step process
  1665. * like scsi-mls remove + put host
  1666. */
  1667. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1668. {
  1669. struct iscsi_conn *conn = cls_conn->dd_data;
  1670. struct iscsi_session *session = conn->session;
  1671. unsigned long flags;
  1672. del_timer_sync(&conn->transport_timer);
  1673. spin_lock_bh(&session->lock);
  1674. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1675. if (session->leadconn == conn) {
  1676. /*
  1677. * leading connection? then give up on recovery.
  1678. */
  1679. session->state = ISCSI_STATE_TERMINATE;
  1680. wake_up(&conn->ehwait);
  1681. }
  1682. spin_unlock_bh(&session->lock);
  1683. /*
  1684. * Block until all in-progress commands for this connection
  1685. * time out or fail.
  1686. */
  1687. for (;;) {
  1688. spin_lock_irqsave(session->host->host_lock, flags);
  1689. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1690. spin_unlock_irqrestore(session->host->host_lock, flags);
  1691. break;
  1692. }
  1693. spin_unlock_irqrestore(session->host->host_lock, flags);
  1694. msleep_interruptible(500);
  1695. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1696. "host_failed %d\n", session->host->host_busy,
  1697. session->host->host_failed);
  1698. /*
  1699. * force eh_abort() to unblock
  1700. */
  1701. wake_up(&conn->ehwait);
  1702. }
  1703. /* flush queued up work because we free the connection below */
  1704. iscsi_suspend_tx(conn);
  1705. spin_lock_bh(&session->lock);
  1706. kfree(conn->data);
  1707. kfree(conn->persistent_address);
  1708. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1709. sizeof(void*));
  1710. if (session->leadconn == conn)
  1711. session->leadconn = NULL;
  1712. spin_unlock_bh(&session->lock);
  1713. iscsi_destroy_conn(cls_conn);
  1714. }
  1715. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1716. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1717. {
  1718. struct iscsi_conn *conn = cls_conn->dd_data;
  1719. struct iscsi_session *session = conn->session;
  1720. if (!session) {
  1721. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1722. return -EPERM;
  1723. }
  1724. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1725. session->first_burst > session->max_burst) {
  1726. printk("iscsi: invalid burst lengths: "
  1727. "first_burst %d max_burst %d\n",
  1728. session->first_burst, session->max_burst);
  1729. return -EINVAL;
  1730. }
  1731. if (conn->ping_timeout && !conn->recv_timeout) {
  1732. printk(KERN_ERR "iscsi: invalid recv timeout of zero "
  1733. "Using 5 seconds\n.");
  1734. conn->recv_timeout = 5;
  1735. }
  1736. if (conn->recv_timeout && !conn->ping_timeout) {
  1737. printk(KERN_ERR "iscsi: invalid ping timeout of zero "
  1738. "Using 5 seconds.\n");
  1739. conn->ping_timeout = 5;
  1740. }
  1741. spin_lock_bh(&session->lock);
  1742. conn->c_stage = ISCSI_CONN_STARTED;
  1743. session->state = ISCSI_STATE_LOGGED_IN;
  1744. session->queued_cmdsn = session->cmdsn;
  1745. conn->last_recv = jiffies;
  1746. conn->last_ping = jiffies;
  1747. if (conn->recv_timeout && conn->ping_timeout)
  1748. mod_timer(&conn->transport_timer,
  1749. jiffies + (conn->recv_timeout * HZ));
  1750. switch(conn->stop_stage) {
  1751. case STOP_CONN_RECOVER:
  1752. /*
  1753. * unblock eh_abort() if it is blocked. re-try all
  1754. * commands after successful recovery
  1755. */
  1756. conn->stop_stage = 0;
  1757. conn->tmf_state = TMF_INITIAL;
  1758. session->age++;
  1759. spin_unlock_bh(&session->lock);
  1760. iscsi_unblock_session(session_to_cls(session));
  1761. wake_up(&conn->ehwait);
  1762. return 0;
  1763. case STOP_CONN_TERM:
  1764. conn->stop_stage = 0;
  1765. break;
  1766. default:
  1767. break;
  1768. }
  1769. spin_unlock_bh(&session->lock);
  1770. return 0;
  1771. }
  1772. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1773. static void
  1774. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1775. {
  1776. struct iscsi_mgmt_task *mtask, *tmp;
  1777. /* handle pending */
  1778. list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
  1779. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1780. iscsi_free_mgmt_task(conn, mtask);
  1781. }
  1782. /* handle running */
  1783. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1784. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1785. iscsi_free_mgmt_task(conn, mtask);
  1786. }
  1787. conn->mtask = NULL;
  1788. }
  1789. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1790. struct iscsi_conn *conn, int flag)
  1791. {
  1792. int old_stop_stage;
  1793. del_timer_sync(&conn->transport_timer);
  1794. mutex_lock(&session->eh_mutex);
  1795. spin_lock_bh(&session->lock);
  1796. if (conn->stop_stage == STOP_CONN_TERM) {
  1797. spin_unlock_bh(&session->lock);
  1798. mutex_unlock(&session->eh_mutex);
  1799. return;
  1800. }
  1801. /*
  1802. * The LLD either freed/unset the lock on us, or userspace called
  1803. * stop but did not create a proper connection (connection was never
  1804. * bound or it was unbound then stop was called).
  1805. */
  1806. if (!conn->recv_lock) {
  1807. spin_unlock_bh(&session->lock);
  1808. mutex_unlock(&session->eh_mutex);
  1809. return;
  1810. }
  1811. /*
  1812. * When this is called for the in_login state, we only want to clean
  1813. * up the login task and connection. We do not need to block and set
  1814. * the recovery state again
  1815. */
  1816. if (flag == STOP_CONN_TERM)
  1817. session->state = ISCSI_STATE_TERMINATE;
  1818. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1819. session->state = ISCSI_STATE_IN_RECOVERY;
  1820. old_stop_stage = conn->stop_stage;
  1821. conn->stop_stage = flag;
  1822. conn->c_stage = ISCSI_CONN_STOPPED;
  1823. spin_unlock_bh(&session->lock);
  1824. iscsi_suspend_tx(conn);
  1825. write_lock_bh(conn->recv_lock);
  1826. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1827. write_unlock_bh(conn->recv_lock);
  1828. /*
  1829. * for connection level recovery we should not calculate
  1830. * header digest. conn->hdr_size used for optimization
  1831. * in hdr_extract() and will be re-negotiated at
  1832. * set_param() time.
  1833. */
  1834. if (flag == STOP_CONN_RECOVER) {
  1835. conn->hdrdgst_en = 0;
  1836. conn->datadgst_en = 0;
  1837. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1838. old_stop_stage != STOP_CONN_RECOVER) {
  1839. debug_scsi("blocking session\n");
  1840. iscsi_block_session(session_to_cls(session));
  1841. }
  1842. }
  1843. /*
  1844. * flush queues.
  1845. */
  1846. spin_lock_bh(&session->lock);
  1847. fail_all_commands(conn, -1);
  1848. flush_control_queues(session, conn);
  1849. spin_unlock_bh(&session->lock);
  1850. mutex_unlock(&session->eh_mutex);
  1851. }
  1852. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1853. {
  1854. struct iscsi_conn *conn = cls_conn->dd_data;
  1855. struct iscsi_session *session = conn->session;
  1856. switch (flag) {
  1857. case STOP_CONN_RECOVER:
  1858. case STOP_CONN_TERM:
  1859. iscsi_start_session_recovery(session, conn, flag);
  1860. break;
  1861. default:
  1862. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1863. }
  1864. }
  1865. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1866. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1867. struct iscsi_cls_conn *cls_conn, int is_leading)
  1868. {
  1869. struct iscsi_session *session = class_to_transport_session(cls_session);
  1870. struct iscsi_conn *conn = cls_conn->dd_data;
  1871. spin_lock_bh(&session->lock);
  1872. if (is_leading)
  1873. session->leadconn = conn;
  1874. spin_unlock_bh(&session->lock);
  1875. /*
  1876. * Unblock xmitworker(), Login Phase will pass through.
  1877. */
  1878. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1879. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1880. return 0;
  1881. }
  1882. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1883. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1884. enum iscsi_param param, char *buf, int buflen)
  1885. {
  1886. struct iscsi_conn *conn = cls_conn->dd_data;
  1887. struct iscsi_session *session = conn->session;
  1888. uint32_t value;
  1889. switch(param) {
  1890. case ISCSI_PARAM_FAST_ABORT:
  1891. sscanf(buf, "%d", &session->fast_abort);
  1892. break;
  1893. case ISCSI_PARAM_ABORT_TMO:
  1894. sscanf(buf, "%d", &session->abort_timeout);
  1895. break;
  1896. case ISCSI_PARAM_LU_RESET_TMO:
  1897. sscanf(buf, "%d", &session->lu_reset_timeout);
  1898. break;
  1899. case ISCSI_PARAM_PING_TMO:
  1900. sscanf(buf, "%d", &conn->ping_timeout);
  1901. break;
  1902. case ISCSI_PARAM_RECV_TMO:
  1903. sscanf(buf, "%d", &conn->recv_timeout);
  1904. break;
  1905. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1906. sscanf(buf, "%d", &conn->max_recv_dlength);
  1907. break;
  1908. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1909. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1910. break;
  1911. case ISCSI_PARAM_HDRDGST_EN:
  1912. sscanf(buf, "%d", &conn->hdrdgst_en);
  1913. break;
  1914. case ISCSI_PARAM_DATADGST_EN:
  1915. sscanf(buf, "%d", &conn->datadgst_en);
  1916. break;
  1917. case ISCSI_PARAM_INITIAL_R2T_EN:
  1918. sscanf(buf, "%d", &session->initial_r2t_en);
  1919. break;
  1920. case ISCSI_PARAM_MAX_R2T:
  1921. sscanf(buf, "%d", &session->max_r2t);
  1922. break;
  1923. case ISCSI_PARAM_IMM_DATA_EN:
  1924. sscanf(buf, "%d", &session->imm_data_en);
  1925. break;
  1926. case ISCSI_PARAM_FIRST_BURST:
  1927. sscanf(buf, "%d", &session->first_burst);
  1928. break;
  1929. case ISCSI_PARAM_MAX_BURST:
  1930. sscanf(buf, "%d", &session->max_burst);
  1931. break;
  1932. case ISCSI_PARAM_PDU_INORDER_EN:
  1933. sscanf(buf, "%d", &session->pdu_inorder_en);
  1934. break;
  1935. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1936. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1937. break;
  1938. case ISCSI_PARAM_ERL:
  1939. sscanf(buf, "%d", &session->erl);
  1940. break;
  1941. case ISCSI_PARAM_IFMARKER_EN:
  1942. sscanf(buf, "%d", &value);
  1943. BUG_ON(value);
  1944. break;
  1945. case ISCSI_PARAM_OFMARKER_EN:
  1946. sscanf(buf, "%d", &value);
  1947. BUG_ON(value);
  1948. break;
  1949. case ISCSI_PARAM_EXP_STATSN:
  1950. sscanf(buf, "%u", &conn->exp_statsn);
  1951. break;
  1952. case ISCSI_PARAM_USERNAME:
  1953. kfree(session->username);
  1954. session->username = kstrdup(buf, GFP_KERNEL);
  1955. if (!session->username)
  1956. return -ENOMEM;
  1957. break;
  1958. case ISCSI_PARAM_USERNAME_IN:
  1959. kfree(session->username_in);
  1960. session->username_in = kstrdup(buf, GFP_KERNEL);
  1961. if (!session->username_in)
  1962. return -ENOMEM;
  1963. break;
  1964. case ISCSI_PARAM_PASSWORD:
  1965. kfree(session->password);
  1966. session->password = kstrdup(buf, GFP_KERNEL);
  1967. if (!session->password)
  1968. return -ENOMEM;
  1969. break;
  1970. case ISCSI_PARAM_PASSWORD_IN:
  1971. kfree(session->password_in);
  1972. session->password_in = kstrdup(buf, GFP_KERNEL);
  1973. if (!session->password_in)
  1974. return -ENOMEM;
  1975. break;
  1976. case ISCSI_PARAM_TARGET_NAME:
  1977. /* this should not change between logins */
  1978. if (session->targetname)
  1979. break;
  1980. session->targetname = kstrdup(buf, GFP_KERNEL);
  1981. if (!session->targetname)
  1982. return -ENOMEM;
  1983. break;
  1984. case ISCSI_PARAM_TPGT:
  1985. sscanf(buf, "%d", &session->tpgt);
  1986. break;
  1987. case ISCSI_PARAM_PERSISTENT_PORT:
  1988. sscanf(buf, "%d", &conn->persistent_port);
  1989. break;
  1990. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1991. /*
  1992. * this is the address returned in discovery so it should
  1993. * not change between logins.
  1994. */
  1995. if (conn->persistent_address)
  1996. break;
  1997. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1998. if (!conn->persistent_address)
  1999. return -ENOMEM;
  2000. break;
  2001. default:
  2002. return -ENOSYS;
  2003. }
  2004. return 0;
  2005. }
  2006. EXPORT_SYMBOL_GPL(iscsi_set_param);
  2007. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  2008. enum iscsi_param param, char *buf)
  2009. {
  2010. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  2011. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2012. int len;
  2013. switch(param) {
  2014. case ISCSI_PARAM_FAST_ABORT:
  2015. len = sprintf(buf, "%d\n", session->fast_abort);
  2016. break;
  2017. case ISCSI_PARAM_ABORT_TMO:
  2018. len = sprintf(buf, "%d\n", session->abort_timeout);
  2019. break;
  2020. case ISCSI_PARAM_LU_RESET_TMO:
  2021. len = sprintf(buf, "%d\n", session->lu_reset_timeout);
  2022. break;
  2023. case ISCSI_PARAM_INITIAL_R2T_EN:
  2024. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  2025. break;
  2026. case ISCSI_PARAM_MAX_R2T:
  2027. len = sprintf(buf, "%hu\n", session->max_r2t);
  2028. break;
  2029. case ISCSI_PARAM_IMM_DATA_EN:
  2030. len = sprintf(buf, "%d\n", session->imm_data_en);
  2031. break;
  2032. case ISCSI_PARAM_FIRST_BURST:
  2033. len = sprintf(buf, "%u\n", session->first_burst);
  2034. break;
  2035. case ISCSI_PARAM_MAX_BURST:
  2036. len = sprintf(buf, "%u\n", session->max_burst);
  2037. break;
  2038. case ISCSI_PARAM_PDU_INORDER_EN:
  2039. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  2040. break;
  2041. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  2042. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  2043. break;
  2044. case ISCSI_PARAM_ERL:
  2045. len = sprintf(buf, "%d\n", session->erl);
  2046. break;
  2047. case ISCSI_PARAM_TARGET_NAME:
  2048. len = sprintf(buf, "%s\n", session->targetname);
  2049. break;
  2050. case ISCSI_PARAM_TPGT:
  2051. len = sprintf(buf, "%d\n", session->tpgt);
  2052. break;
  2053. case ISCSI_PARAM_USERNAME:
  2054. len = sprintf(buf, "%s\n", session->username);
  2055. break;
  2056. case ISCSI_PARAM_USERNAME_IN:
  2057. len = sprintf(buf, "%s\n", session->username_in);
  2058. break;
  2059. case ISCSI_PARAM_PASSWORD:
  2060. len = sprintf(buf, "%s\n", session->password);
  2061. break;
  2062. case ISCSI_PARAM_PASSWORD_IN:
  2063. len = sprintf(buf, "%s\n", session->password_in);
  2064. break;
  2065. default:
  2066. return -ENOSYS;
  2067. }
  2068. return len;
  2069. }
  2070. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  2071. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  2072. enum iscsi_param param, char *buf)
  2073. {
  2074. struct iscsi_conn *conn = cls_conn->dd_data;
  2075. int len;
  2076. switch(param) {
  2077. case ISCSI_PARAM_PING_TMO:
  2078. len = sprintf(buf, "%u\n", conn->ping_timeout);
  2079. break;
  2080. case ISCSI_PARAM_RECV_TMO:
  2081. len = sprintf(buf, "%u\n", conn->recv_timeout);
  2082. break;
  2083. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  2084. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  2085. break;
  2086. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  2087. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  2088. break;
  2089. case ISCSI_PARAM_HDRDGST_EN:
  2090. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  2091. break;
  2092. case ISCSI_PARAM_DATADGST_EN:
  2093. len = sprintf(buf, "%d\n", conn->datadgst_en);
  2094. break;
  2095. case ISCSI_PARAM_IFMARKER_EN:
  2096. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  2097. break;
  2098. case ISCSI_PARAM_OFMARKER_EN:
  2099. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  2100. break;
  2101. case ISCSI_PARAM_EXP_STATSN:
  2102. len = sprintf(buf, "%u\n", conn->exp_statsn);
  2103. break;
  2104. case ISCSI_PARAM_PERSISTENT_PORT:
  2105. len = sprintf(buf, "%d\n", conn->persistent_port);
  2106. break;
  2107. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  2108. len = sprintf(buf, "%s\n", conn->persistent_address);
  2109. break;
  2110. default:
  2111. return -ENOSYS;
  2112. }
  2113. return len;
  2114. }
  2115. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  2116. int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2117. char *buf)
  2118. {
  2119. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2120. int len;
  2121. switch (param) {
  2122. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2123. if (!session->netdev)
  2124. len = sprintf(buf, "%s\n", "default");
  2125. else
  2126. len = sprintf(buf, "%s\n", session->netdev);
  2127. break;
  2128. case ISCSI_HOST_PARAM_HWADDRESS:
  2129. if (!session->hwaddress)
  2130. len = sprintf(buf, "%s\n", "default");
  2131. else
  2132. len = sprintf(buf, "%s\n", session->hwaddress);
  2133. break;
  2134. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2135. if (!session->initiatorname)
  2136. len = sprintf(buf, "%s\n", "unknown");
  2137. else
  2138. len = sprintf(buf, "%s\n", session->initiatorname);
  2139. break;
  2140. default:
  2141. return -ENOSYS;
  2142. }
  2143. return len;
  2144. }
  2145. EXPORT_SYMBOL_GPL(iscsi_host_get_param);
  2146. int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  2147. char *buf, int buflen)
  2148. {
  2149. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  2150. switch (param) {
  2151. case ISCSI_HOST_PARAM_NETDEV_NAME:
  2152. if (!session->netdev)
  2153. session->netdev = kstrdup(buf, GFP_KERNEL);
  2154. break;
  2155. case ISCSI_HOST_PARAM_HWADDRESS:
  2156. if (!session->hwaddress)
  2157. session->hwaddress = kstrdup(buf, GFP_KERNEL);
  2158. break;
  2159. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  2160. if (!session->initiatorname)
  2161. session->initiatorname = kstrdup(buf, GFP_KERNEL);
  2162. break;
  2163. default:
  2164. return -ENOSYS;
  2165. }
  2166. return 0;
  2167. }
  2168. EXPORT_SYMBOL_GPL(iscsi_host_set_param);
  2169. MODULE_AUTHOR("Mike Christie");
  2170. MODULE_DESCRIPTION("iSCSI library functions");
  2171. MODULE_LICENSE("GPL");