In the first film, Agent Smith spends a lot of time torturing Morpheus because he knew the code for Zion's mainframe. My question is, why did they even need that code? I mean, surely they would already know where Zion is. The previous One did after-all pick the people to remake Zion, and you would think the Machines would know where the new Zion would be. Someone explain?
All of the machine characters we see in the movie are just doing the job they have been programmed to do, without questioning it. Even characters like The Oracle, who acts as a sort of counter-agent to the Matrix, is actually designed to be that way; she is the program built to represent the idea of free will, vs. the architect, who represents predestination.
Logically, the machine-level programs only know what they need to know to perform their routine tasks. It's highly unlikely that the Agents have any idea that the Matrix itself already knows where Zion is. Instead, they are following their programming, which is to track down and eliminate the free humans, and part of that job means finding out where they are hiding outside the matrix.
This really just begs the question, though ; what we really need to answer is, why were they were programmed that way? To figure that out, you need to understand something that was not well explained in the movies, but is more clearly laid out in the supplemental material: this was the third Matrix attempt. The first two failed because humans are inherently not fully logical beings -- their brains will not just "accept reality" as presented to them, it has to choose their reality. This concept of giving the plugged-in brains the freedom to act as the want is the reason the third Matrix iteration survived.
However, this freedom leads to a number of problems, the main one being that certain minds choose not to accept the reality of the Matrix. The Architect explains that, for whatever reason, the necessity of free will in the Matrix means they also have to give the humans the freedom to escape, or the "glitches" brought about by their rejection will eventually crash the system. But obviously they can't just let everyone out, or eventually they won't have anyone left. So, they set up the system we see in the third movie: every so often, the matrix spits out The One, as a reset switch, and the machines kill everyone else and the whole cycle starts over.
One major problem with this is that getting that cycle to end successfully means somehow getting all of the humans involved to make the correct choices, up to and including The One. In fact, the Architect's speech implies that everything that happened in the first movie, including Neo rescuing Morpheus from the Agents, was part of the plan, and had to happen in order to move towards a reset of the cycle. Everything we see the human agents do in the movies was something The Matrix wanted to be done, so it needs to take every precaution to make sure those humans stay on the right path.
Requiring the humans to all behave exactly as The Matrix wants means setting up a scenario where those actions are the natural decisions for the humans to make. Things need to play out in such a way that The One does not figure out what's going on until it's too late to do anything about it. Part of this facade includes programming lower-level programs, like Agents, that really believe they are hunting for humans, so the humans have a legitimate enemy to struggle against.
The concept explained within the Matrix movies is that the machines and their programs are performing a sort of 'dance' with the humans of zion in a repeating process of events. The need to capture and torture Morpheus is necessary despite their lack of need for the codes for the illusion of the events being a single first time occurrence to hold.
Even if they knew where Zion was, it's still a secure structure, which is why they needed to tunnel into it instead of going through the front gates (which would be better guarded but an already-open vector of attack). The open-close mechanism on the main doors were apparently computer-controlled, and Cypher told Agent Smith in the restaurant that he had the gate codes but not the mainframe codes.
I always assumed that the "codes" meant whatever credentials were required to gain access remotely, not necessarily a common passphrase or key; and that there was a hierarchy, where if you gained access to the mainframe codes you would also gain access to the gate codes, but not vice versa. That being said; if the Agents were able to gain access to the mainframe, even if they weren't able to control the gate from it, they would still be able to wreak havoc on the inside and help break down any defenses (in a world where a lot of the critical infrastructure is also computer-controlled).
Worst case scenario, I would envision that gaining access to the mainframe / central systems controller would allow them to do things such as but not limited to: open the city's front gates to let the sentinels fly right in, disrupt water and air filtration / scrubbing (they showed the water / air systems in the 2nd or 3rd movie) to put pressure on the people inside and limit their ability to defend the city effectively, take down any early-warning systems for sentinel attacks, etc.
Any such advantage would help the machines in an attack, thought after reading all the arguments for the Agents just following their programming and not knowing any better, I can't tell if the Agents saw the Zion codes as a mission-critical aspect that needed to be retrieved, or simply another advantage they were looking to gain. Though if it really was 100% about following their programming to drive the One cycle, as a lot of people make good arguments for, how relevant would any of these reasons even be?
Proper functioning of the Matrix depends, in part, on an illusion of freedom. If that illusion is not carefully maintained, then humanity rejects it en masse, and the system collapses. As The Architect states in The Matrix Reloaded, even such figures as the Oracle and The One himself are actually part of the Matrix, serving necessary functions that the Machines did not originally know about, but have since adapted to.
Therefore, even though the Matrix already has everything it needs to know about Zion, the humans must not know that it knows. The Agents try to get Zion's mainframe codes because that is what The Matrix would do if it didn't have the codes, but it's all theater. The Matrix bides its time and goes about its business until it is due for another collapse, but part of that business is to tend to the "free" Humans and ensure that they still think they are free.
It is unclear whether or not the Agents know this. From a pragmatic standpoint -and the Machines are nothing if not pragmatic- it would probably be better that they not know. If they did, then they might unknowingly give off clues that the free humans could pick up on, which would hasten the collapse. The Architect did admit that the Machines "have become exceedingly efficent" at maintaining the cycle, which implies that the process has had to be refined over time. So it's even possible that Agents used to know in early iterations, but accidentally brought about a collapse because of it, and so in more recent iterations they are no longer given that knowledge.